<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.prime.com.tr/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                template="./../template/MemberTemplate.xhtml">

    <ui:define name="contentInsert">
               
        <h3>Rewards</h3>
        Reward vouchers can be purchased using loyalty points. A customer's points will expire if he does not make any new <br/>
        purchases within 5 years. The expiry date is taken to be 5 years after the most recent item purchase transaction.
        <br/>
        <p:messages id="buyVoucherMessage" showDetail="true"/> 
        
        <br/><br/>

        <h4>Points Status: </h4>
        <h:outputLabel value="#{rewardMB.getPointsStatus()}" escape = "false"/>
        
        <br/><br/>
        
        <h4>Rewards on offer: </h4>
        NOTE: You will only be able to purchase a reward voucher if you have enough points for it.
        <h:form>
            <p:dataTable value="#{rewardMB.getAllRewards()}" var="o"
                                     emptyMessage="No rewards to display"
                                     >

                <p:column style="width: 20%">
                    <f:facet name="header">Name</f:facet>
                                        #{o.reward_Name}
                </p:column>

                <p:column style="width: 40%">
                    <f:facet name="header">Description</f:facet>
                    <h:outputText value="#{o.reward_Description}" escape="false"/>
                </p:column> 

                <p:column style="width: 10%">
                    <f:facet name="header">Points Cost</f:facet>
                                        #{o.reward_Cost}
                </p:column> 

                <p:column style="width: 10%">
                    <f:facet name="header">Vouchers Available</f:facet>
                                        #{o.getRemainingVouchers()}
                </p:column>

                <p:column style="width: 20%">
                    <p:commandButton  value="Buy this Reward!" action="#{rewardMB.buyRewardVoucher(o.id)}" rendered="#{rewardMB.cannotAfford(o.reward_Cost)}" ajax="false"/>     
                </p:column> 
            </p:dataTable>
        </h:form>
        
        <br/><br/>

        
        <h4>Buy Loyalty Points: </h4>
        <h:panelGroup id="selectedChoice">
            <h:form>                                                 
                <p:dataTable var="selectedRow" value="#{CustomerPointsMB.purchaseList}"
                             rowSelectListener="#{CustomerPointsMB.updatePurchasePrice()}"
                             selection="#{CustomerPointsMB.selectedPurchaseItem}" selectionMode="single" 
                             onRowSelectUpdate="selectedChoice"
                             >
                    <p:column headerText="Points"> #{selectedRow.quantity}</p:column>          
                    <p:column headerText="Price"> #{selectedRow.price}</p:column>  
                </p:dataTable>                      
            </h:form>   
            
            
            <form id="id_PaypalForm" action="https://www.sandbox.paypal.com/cgi-bin/webscr" method="post" 
                  style="margin-left:auto;margin-right:auto;">  

                <input type="hidden" name="cmd" value="_xclick"/>  
                <input type="hidden" name="business" value="#{returnURLMB.tCubePayPalEmail}"/>  
                <input type="hidden" name="item_name"   value="Loyalty Points Purchase"/>  
                <input type="hidden" name="item_number" value="0"/>  
                <input type="hidden" name="amount" value="#{CustomerPointsMB.selectedPurchaseItem.price}"/>  
                <input type="hidden" name="tax" value="0"/>  
                <input type="hidden" name="quantity" value="1"/>  
                <input type="hidden" name="no_note" value="1"/>  
                <input type="hidden" name="currency_code" value="SGD"/> 
                <input type="hidden" name="return" value="#{returnURLMB.getReturnURL_userBuyPoints()}"/> 

                <input type="image" name="submit" border="0"
                       src="https://www.paypal.com/en_US/i/btn/btn_buynow_LG.gif"  
                       alt="PayPal - The safer, easier way to pay online"
                       />  
            </form>  
        </h:panelGroup>

        
        

    </ui:define>
</ui:composition>

